package com.yanqu.road.server.cmd.activity.metaspace;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.pb.activity.MetaSpaceProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.metaspace.MetaspaceModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.Map;

@Cmd(code = GameProtocol.S_META_SPACE_CROSS_REWARD,desc = "")
public class MetaSpaceRewardFromCrossCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        MetaSpaceProto.SendRewardMsgToServerMsg req = MetaSpaceProto.SendRewardMsgToServerMsg.parseFrom(packet.getBytes());
        int activityId = req.getActivityId();
        int conditionId = req.getConditionId();
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (ActivityHelper.activityInTime(activityInfo)) {
            ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfo(activityId, conditionId);
            if (conditionInfo != null) {
                Property reward = conditionInfo.getReward();
                Property realReward = new Property();
                for (Map.Entry<Integer, BigInteger> entry : reward.getGoods().entrySet()) {
                    Integer goodsId = entry.getKey();
                    BigInteger value = entry.getValue();
                    GoodsInfo goodsInfo = GoodsMgr.getGoodsById(goodsId);
                    if (goodsInfo == null) {
                        continue;
                    }
                    if (goodsInfo.getType() == 3) {
                        // 是宝箱
                        for (int i = 0; i < value.intValue(); i++) {
                            OpenGoodsBagResult randomGoods = GoodsBagMgr.getRandomGoods(goodsId);
                            realReward.addProperty(randomGoods.getReward());
                        }
                    }else {
                        realReward.addProperty(goodsId, value);
                    }
                }
                player.getModule(MetaspaceModule.class).addRewardInfo(activityId, PropertyHelper.parsePropertyToString(realReward));
            }
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
